package com.yizisu.library.ad.impl.ironsource

import androidx.lifecycle.MutableLiveData
import com.ironsource.mediationsdk.logger.IronSourceError
import com.ironsource.mediationsdk.sdk.OfferwallListener

import com.yizisu.library.ad.IAdRewardStatus
import com.yizisu.library.ad.IAdStatus
import java.lang.ref.WeakReference

object IronSourceOfferWallListener : OfferwallListener {
    var rewardListener: WeakReference<Function1<IAdRewardStatus, Unit>>? = null
    val statusData = MutableLiveData<IAdStatus>()
    var offerWallListener: Function3<Int, Int, Boolean, Boolean>? = null

    override fun onOfferwallAvailable(available: Boolean) {
        if (available) {
            statusData.postValue(IAdStatus.Success)
        } else {
            statusData.postValue(IAdStatus.Error)
        }
    }

    override fun onOfferwallOpened() {
        rewardListener?.get()?.invoke(IAdRewardStatus.Success)
        rewardListener?.clear()
    }

    override fun onOfferwallShowFailed(p0: IronSourceError?) {
        statusData.postValue(IAdStatus.Error)
    }

    override fun onOfferwallAdCredited(
        credits: Int/*用户可领取的积分*/,
        totalCredits: Int/*曾经获得的总积分*/,
        totalCreditsFlag: Boolean/*提供数据是否准确*/
    ): Boolean {
        return offerWallListener?.invoke(credits, totalCredits, totalCreditsFlag) ?: false
    }

    override fun onGetOfferwallCreditsFailed(p0: IronSourceError?) {

    }

    override fun onOfferwallClosed() {

    }
}